package com.kefu.chat.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.chat.model.ChatVideos;
import com.kefu.chat.service.ChatVideosService;
import com.kefu.chat.vo.ChatVideosSearchVo;
import com.kefu.chat.vo.ChatVideosInsertVo;
import com.kefu.chat.vo.ChatVideosUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author hxh
 * @date 2022/10/26
 */
@RestController
@Api(tags = "音视频接口")
@RequestMapping("chat/videos")
public class ChatVideosController extends AbstractBaseController {

    @Autowired
    private ChatVideosService chatVideosService;

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public Result<String> add(@Validated @RequestBody ChatVideosInsertVo chatVideosVo) {
    	logger.info("新增:{}", chatVideosVo);
        return ResultGenerator.genSuccessResult( chatVideosService.save(chatVideosVo));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除:{}", delete);
    	
        chatVideosService.removeById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public Result<String> update(@Validated @RequestBody ChatVideosUpdateVo chatVideosVo) {
    	logger.info("修改:{}", chatVideosVo);
    	
    	ChatVideos info = chatVideosVo.convert();
        chatVideosService.updateById(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情")
    public Result<ChatVideos> detail(@Validated @RequestBody DetailVo detail) {

        ChatVideos chatVideos = chatVideosService.getById(detail.getId());
        return ResultGenerator.genSuccessResult(chatVideos);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询")
    public Result<Page<ChatVideos>> list(@Validated @RequestBody ChatVideosSearchVo search) {    

        Page<ChatVideos> page = chatVideosService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("/videoStatistics")
    @ApiOperation("音视频通话统计")
    public  Result<List<ChatVideos>>  videoStatistics(@Validated @RequestBody ChatVideosSearchVo vo){
        return ResultGenerator.genSuccessResult( chatVideosService.videoStatistics(vo));
    }

    @PostMapping("/videoStatisticsExport")
    @ApiOperation("导出音视频通话统计")
    public void videoStatisticsExport(@Validated @RequestBody ChatVideosSearchVo vo, HttpServletResponse response) throws IOException {
        chatVideosService.videoStatisticsExport(vo,response);
    }

    @PostMapping("/videoAndAudioTotalLineStatistics")
    @ApiOperation("音视频通话折线统计")
    public  Result<List<ChatVideos>>  videoAndAudioTotalLineStatistics(@Validated @RequestBody ChatVideosSearchVo vo){

        return ResultGenerator.genSuccessResult( chatVideosService.videoAndAudioTotalLineStatistics(vo));
    }

}
